﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using FlexCell;
using YArchitech.LIB;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;

namespace SectionSignAndFilled.MaterialManager
{
	public partial class ConfigFrom : System.Windows.Forms.Form
	{
		public ConfigFrom(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.document = doc;
			this.MaterialMan = new MateriaDataManger(this.document);
			List<BaseLinkDocumentResultBean> needDealEle = this.GetNeedDealEle(this.document);
			this.MaterialMan.GetAllMaterial(needDealEle);
			this.SetGridStyle();
			this.LoadTableData();
		}

		private List<BaseLinkDocumentResultBean> GetNeedDealEle(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollectorLinkDocument filteredElementCollectorLinkDocument = new FilteredElementCollectorLinkDocument(doc);
			ElementCategoryFilter item = new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns);
			ElementCategoryFilter item2 = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
			ElementClassFilter item3 = new ElementClassFilter(typeof(Wall));
			ElementClassFilter item4 = new ElementClassFilter(typeof(Floor));
			ElementClassFilter item5 = new ElementClassFilter(typeof(RoofBase));
			ElementClassFilter item6 = new ElementClassFilter(typeof(Stairs));
			LogicalOrFilter filter = new LogicalOrFilter(new List<ElementFilter>
			{
				item3,
				item2,
				item4,
				item,
				item6,
				item5
			});
			filteredElementCollectorLinkDocument.WherePass(filter);
			return filteredElementCollectorLinkDocument.ToBeans();
		}

		private void SetGridStyle()
		{
			this.grid1.Row(0).Visible = false;
			this.grid1.Column(0).Visible = false;
			this.grid1.Rows = this.MaterialMan.Datas.Count + 3;
			this.grid1.Cols = 6;
			double num = (double)base.Width - (double)this.grid1.Top * 3.0;
			this.grid1.Column(1).Width = (short)(num * 0.28);
			this.grid1.Column(2).Width = (short)(num * 0.18);
			this.grid1.Column(3).Width = (short)(num * 0.18);
			this.grid1.Column(4).Width = (short)(num * 0.18);
			this.grid1.Column(5).Width = (short)(num * 0.18);
			for (int i = 0; i < this.grid1.Rows; i++)
			{
				this.grid1.Row(i).Height = 40;
			}
            this.grid1.BackColorBkg = System.Drawing.Color.FromArgb(255, 255, 255);
			this.grid1.Range(1, 1, 2, 1).Merge();
			this.grid1.Range(1, 2, 1, 3).Merge();
			this.grid1.Range(1, 4, 1, 5).Merge();
			this.grid1.Locked = true;
			this.grid1.ShowResizeTip = true;
			this.grid1.Refresh();
			this.grid1.Cell(1, 1).Text = "材质";
			this.grid1.Cell(1, 1).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(1, 2).Text = "大于1：100";
			this.grid1.Cell(1, 2).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(1, 4).Text = "小于等于1：100";
			this.grid1.Cell(1, 4).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(2, 2).Text = "样式";
			this.grid1.Cell(2, 2).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(2, 3).Text = "颜色";
			this.grid1.Cell(2, 3).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(2, 4).Text = "样式";
			this.grid1.Cell(2, 4).Alignment = AlignmentEnum.LeftGeneral;
			this.grid1.Cell(2, 5).Text = "颜色";
			this.grid1.Cell(2, 5).Alignment = AlignmentEnum.LeftGeneral;
		}

		private bool LoadTableData()
		{
			for (int i = 0; i < this.MaterialMan.Datas.Count; i++)
			{
				MateriaData materiaData = this.MaterialMan.Datas[i];
				this.grid1.Cell(i + 3, 1).Text = materiaData.Name;
				this.grid1.Cell(i + 3, 1).WrapText = true;
				if (materiaData.ScalesData.Count == 2)
				{
					Row row = this.grid1.Row(i + 3);
					Column column = this.grid1.Column(2);
					Cell cellGrid = this.grid1.Cell(i + 3, 2);
					Cell cellGrid2 = this.grid1.Cell(i + 3, 3);
					materiaData.ScalesData[0].rowInGrid = i + 3;
					materiaData.ScalesData[0].colInGrid = 2;
					this.SetFillPatternToGridCell(cellGrid, materiaData.ScalesData[0].GetFillPattern(), (double)row.Height, (double)column.Width);
                    Autodesk.Revit.DB.Color colorModify = materiaData.ScalesData[0].ColorModify;
                    this.SetColorToGridCell(cellGrid2, System.Drawing.Color.FromArgb((int)colorModify.Red, (int)colorModify.Green, (int)colorModify.Blue));
					Cell cellGrid3 = this.grid1.Cell(i + 3, 4);
					Cell cellGrid4 = this.grid1.Cell(i + 3, 5);
					materiaData.ScalesData[1].rowInGrid = i + 3;
					materiaData.ScalesData[1].colInGrid = 4;
					this.SetFillPatternToGridCell(cellGrid3, materiaData.ScalesData[1].GetFillPattern(), (double)row.Height, (double)column.Width);
					colorModify = materiaData.ScalesData[1].ColorModify;
                    this.SetColorToGridCell(cellGrid4, System.Drawing.Color.FromArgb((int)colorModify.Red, (int)colorModify.Green, (int)colorModify.Blue));
				}
			}
			return true;
		}

		private bool SetFillPatternToGridCell(Cell cellGrid, FillPattern fillPattern, double heightCell, double widthCell)
		{
			if (fillPattern == null || cellGrid == null)
			{
				return false;
			}
			string text = Tools.CreateFillPatternImageRePath(fillPattern, widthCell, heightCell);
			if (text == null || text == "")
			{
				return false;
			}
			this.grid1.Images.Add(text, text);
			cellGrid.SetImage(text);
			return true;
		}

        private bool SetColorToGridCell(Cell cellGrid, System.Drawing.Color colorBk)
		{
			if (cellGrid == null)
			{
				return false;
			}
			cellGrid.BackColor = colorBk;
			return true;
		}

		private void DoubleClickCell(object Sender, EventArgs e)
		{
			MouseEventArgs mouseEventArgs = e as MouseEventArgs;
			Cell cell = this.grid1.HitTest(mouseEventArgs.X, mouseEventArgs.Y);
			if (cell == null)
			{
				return;
			}
			int col = cell.Col;
			int row = cell.Row;
			if (col < 2 || row < 3)
			{
				return;
			}
			if (col % 2 == 0)
			{
				FillPattern fill = this.MaterialMan.GetFill(row, col);
				FillPatternListForm fillPatternListForm = new FillPatternListForm(this.document, fill);
				if (fillPatternListForm.ShowDialog() == DialogResult.OK)
				{
					FillPattern fillPattern = fillPatternListForm.SelectedFillPattern();
					this.MaterialMan.SetFillName(fillPatternListForm.SelectedFillPattern().Name, row, col);
					this.SetFillPatternToGridCell(cell, fillPattern, (double)this.grid1.Row(row).Height, (double)this.grid1.Column(col).Width);
					return;
				}
			}
			else
			{
				ColorDialog colorDialog = new ColorDialog();
				if (colorDialog.ShowDialog() == DialogResult.OK)
				{
					this.MaterialMan.SetFillColor(colorDialog.Color, row, col);
					this.SetColorToGridCell(cell, colorDialog.Color);
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.MaterialMan.WriteData();
		}

		private void WriteFormData()
		{
			string text = Product.UserDataLocation + "\\SectionSignAndFilled.XML";
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode;
			if (!File.Exists(text))
			{
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("SectionSignAndFilled");
				xmlNode = xmlDocument.AppendChild(newChild2);
			}
			else
			{
				xmlDocument.Load(text);
				xmlNode = xmlDocument.SelectSingleNode("SectionSignAndFilled");
			}
			XmlElement xmlElement = xmlDocument.SelectSingleNode("SectionSignAndFilled/ConfigFrom") as XmlElement;
			if (xmlElement == null)
			{
				xmlElement = xmlDocument.CreateElement("ConfigFrom");
				xmlElement = (xmlNode.AppendChild(xmlElement) as XmlElement);
			}
			xmlElement.SetAttribute("FromX", base.Location.X.ToString());
			xmlElement.SetAttribute("FromY", base.Location.Y.ToString());
			xmlDocument.Save(text);
		}

		private void ReadFormData()
		{
			string text = Product.UserDataLocation + "\\SectionSignAndFilled.XML";
			if (!File.Exists(text))
			{
				return;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlElement xmlElement = xmlDocument.SelectSingleNode("SectionSignAndFilled/ConfigFrom") as XmlElement;
			if (xmlElement == null)
			{
				return;
			}
			int num = Convert.ToInt32(xmlElement.GetAttribute("FromX"));
			int num2 = Convert.ToInt32(xmlElement.GetAttribute("FromY"));
			if (num != 0 && num2 != 0)
			{
				base.Location = new System.Drawing.Point(num, num2);
			}
		}

		private void ClosedConfig(object sender, FormClosedEventArgs e)
		{
			this.WriteFormData();
		}

		private void LoadConfigFrom(object sender, EventArgs e)
		{
			this.ReadFormData();
		}

		private Document document;

		private MateriaDataManger MaterialMan;
	}
}
